<?php
/**
 * 房间管理
 * comfiger
 * 2016-8-28
 **/
namespace Mama\Model;
use Think\Model;
class RoomModel extends Model{
	//定义表名称
	protected $tableName = 'room';
	
	/**
	 * 添加用户信息
	 **/
	public function registerRoomInfo($data){
		return $this->data($data)->add();
	}
	
	/**
	 * 根据条件查询总数
	 **/
	public function getRoomCount($where){
		return $this->alias('room')
					->join('left join mama_village as village on room.villageid = village.villageid')
					->where($where)->count();
	}
	
	/**
	 * 获取用户列表
	 **/
	public function getRoomList($where){
		return $this->alias('room')
					->join('left join mama_village as village on room.villageid = village.villageid')
					->where($where)
					->field('room.*, village.title as villagename')
					->order('roomid desc')
					->page($page, C('PAGE_SIZE'))
					->select();
	}
	
	/**
	 * 根据条件查询总数
	 **/
	public function getLandlordRoomCount($where){
		return $this->alias('room')
					->join('left join mama_village as village on room.villageid = village.villageid')
					->join('left join mama_room_landlord as landlord on room.roomid = landlord.roomid')
					->join('left join mama_member as member on landlord.userid = member.userid')
					->where($where)
					->count();
	}
	
	/**
	 * 获取用户列表
	 **/
	public function getLandlordRoomList($where){
		return $this->alias('room')
					->join('left join mama_village as village on room.villageid = village.villageid')
					->join('left join mama_room_landlord as landlord on room.roomid = landlord.roomid')
					->join('left join mama_member as member on landlord.userid = member.userid')
					->field('room.roomid,room.doornumber,room.acreage,room.deposit,room.rent,room.property,room.dayprice,room.status,room.createtime,landlord.rent as brent,landlord.property as bproperty,landlord.deopsit as bdeopsit,member.nickname,member.phone,village.title')
					->where($where)
					->order('roomid desc')
					->page($page, C('PAGE_SIZE'))
					->select();
	}

	/**
	 * 获取详情
	 **/
	public function getRoomInfoByRoomId($roomid){
		return $this->where(array('roomid' => $roomid))->find();
	}

	/**
	 * 编辑分店信息
	 **/
	public function editRoomInfoByRoomId($roomid, $data){
		return $this->where(array('roomid' => $roomid))->setField($data);
	}
	

	public function getRoomInfoByWhere($where=array()){
		return $this->where($where)->find();
	}

	/**
	 * 获取房间列表
	 **/
	public function getRoomListByVillageId($villageid){
		return $this->where(array(
			'villageid' => $villageid,
			'isshow' => 1,
			'isdel' => 0
		))->order('roomid asc')->select();
	}
	
	/** -----------------房间类型表------------------------- **/
	public function addRoomTypeInfo($data){
		return M('roomtype')->data($data)->add();
	}
	
	public function getRoomTypeList(){
		return M('roomtype')->order('roomtype asc')->select();
	}
	
	/** -----------------房间与房东关系表------------------------- **/
	public function addRoomAndLandlordInfo($data){
		return M('room_landlord')->data($data)->add();
	}
	
	public function getRoomAndLandlordInfoByRoomId($roomid){
		$info = M('room_landlord')->where(array(
			'roomid' => $roomid,
		))->find();
		return $info;
	}

	/**
	 * 删除关系
	 **/
	public function deleteRoomLandlordInfo($roomid){
		return M('room_landlord')->where(array(
			'roomid' => $roomid
		))->delete();
	}
}